Thread: Passing a parameter via function pointer

  1. #1
    Registered User
    Join Date
    Jun 2012
    Location
    Göteborg
    Posts
    28

    Passing a parameter via function pointer

    Hi,
    I'm trying to pass a parameter via a function pointer. My first attempt without a parameter works but when adding it, it fails.

    So this is without an in parameter to the function pointer in checkPin():

    Code:
    #include <stdio.h>
    
    void sayHello(void)
    {
        printf("Hello\n");
    }  
    
    void checkPin(const int pin, void (*response)(void))
    {
        if (pin == 0)
        {
           response();
        }
    }
      
    int main()
    {
        checkPin(0, sayHello);    
    
        return 0;
    }
    And here I've added an int to the function:

    Code:
    #include <stdlib.h>
     
    void sayHello(int a)
    {
        printf("Hello\n");
    }  
    
    void checkPin(const int pin, void (*response)(int a))
    {
        int a;
        if (pin == 0)
        {
           response(a);
        }
    }
      
    int main()
    {
        checkPin(0, sayHello(10));    
    
        return 0;
    }
    I get a compile error pointing at the row checkPin(0, sayHello(10));

    error: invalid use of void expression

    And probably how to get the parameter information in checkPin() to response().

  2. #2
    Registered User
    Join Date
    Jun 2012
    Location
    Göteborg
    Posts
    28
    I actually came up with this solution. Maybe this is the way to do it?

    Code:
    #include <stdlib.h>
     
    void sayHello(int a)
    {
        printf("Hello: %d\n", a);
    }  
    
    void checkPin(const int pin, void (*response)(int a), int x)
    {
        if (pin == 0)
        {
           response(x);  // call the callback function
        }
    }
      
    int main()
    {
        checkPin(0, sayHello, 30);    
    
        return 0;
    }

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Your solution looks fine to me.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jun 2012
    Location
    Göteborg
    Posts
    28
    OK, thanks for your comment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trouble passing parameter to function
    By cover2 in forum C Programming
    Replies: 3
    Last Post: 12-14-2012, 03:01 PM
  2. Replies: 5
    Last Post: 03-06-2011, 11:57 AM
  3. Passing an array to a function as a parameter
    By boxden in forum C Programming
    Replies: 1
    Last Post: 03-13-2010, 09:35 AM
  4. Problems passing parameter to function
    By HAssan in forum C Programming
    Replies: 5
    Last Post: 10-21-2008, 02:26 PM
  5. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM

Tags for this Thread